﻿using FB.Enums;
using FB.Helpers;
using FB.Providers.EmployeeInfo;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;

//namespace FB.Membership.Principal
namespace FB.Data.Models
{
    [Serializable]
    public class EmployeeInfoSimple
    {
        [Display(ResourceType = typeof(Resources.Models.EmployeeInfo), Name = "EMPCode")]
        public string EMPCode { get; set; }

        [Display(ResourceType = typeof(Resources.Models.EmployeeInfo), Name = "FirstName")]
        public string FirstName { get; set; }

        [Display(ResourceType = typeof(Resources.Models.EmployeeInfo), Name = "LastName")]
        public string LastName { get; set; }

        [Display(ResourceType = typeof(Resources.Models.EmployeeInfo), Name = "ChineseName")]
        public string ChineseName { get; set; }

        [Display(ResourceType = typeof(Resources.Models.EmployeeInfo), Name = "Gender")]
        public string Gender { get; set; }

        [Display(ResourceType = typeof(Resources.Models.EmployeeInfo), Name = "Alias")]
        public string Alias { get; set; }

        [Display(ResourceType = typeof(Resources.Models.EmployeeInfo), Name = "Email")]
        public string Email { get; set; }

        [Display(ResourceType = typeof(Resources.Models.EmployeeInfo), Name = "Status")]
        public EmployeeStatus Status { get; set; }

        [Display(ResourceType = typeof(Resources.Models.EmployeeInfo), Name = "StatusDesc")]
        public string StatusDesc { get { return EnumHelper.GetDescription(Status); } }

        [Display(ResourceType = typeof(Resources.Models.EmployeeInfo), Name = "UserID")]
        public Guid? UserID { get; set; }

        [Display(ResourceType = typeof(Resources.Models.EmployeeInfo), Name = "UserName")]
        public string UserName { get; set; }
    }

    [Serializable]
    public class EmployeeInfoLite : EmployeeInfoSimple
    {
        [Display(ResourceType = typeof(Resources.Models.EmployeeInfo), Name = "FullName")]
        public string FullName
        {
            get
            {
                //if (!string.IsNullOrEmpty(LastName) || !string.IsNullOrEmpty(FirstName) || !string.IsNullOrEmpty(Alias))
                //{
                //    var n = string.Format("{0} {1} {2}", LastName, FirstName, Alias);
                //    return (!string.IsNullOrEmpty(ChineseName)) ?
                //        string.Format("{0} ({1})", n, ChineseName)
                //        : n;
                //}
                //else
                //    return !string.IsNullOrEmpty(ChineseName) ? ChineseName : string.Empty;
                return EmployeeInfoHelper.FullName(FirstName, LastName, Alias, ChineseName);
            }
        }

        
    }

    [Serializable]
    public class EmployeeInfoBase : EmployeeInfoLite
    {
        [NotMapped]
        [Display(ResourceType = typeof(Resources.Models.EmployeeInfo), Name = "COMName")]
        public string CompanyName { get { return GetAllItemName(this.Companies); } }

        [NotMapped]
        [Display(ResourceType = typeof(Resources.Models.EmployeeInfo), Name = "DEPTName")]
        public string DepartmentName { get { return GetAllItemName(this.Departments); } }

        [NotMapped]
        [Display(ResourceType = typeof(Resources.Models.EmployeeInfo), Name = "POSName")]
        public string PositionName { get { return GetAllItemName(this.Positions); } }

        [NotMapped]
        [Display(ResourceType = typeof(Resources.Models.EmployeeInfo), Name = "TEAMName")]
        public string TeamName { get { return GetAllItemName(this.Teams); } }

        [Display(ResourceType = typeof(Resources.Models.EmployeeInfo), Name = "RefCode")]
        public string RefCode { get; set; }


        public IEnumerable<Company> Companies { get; set; }
        public IEnumerable<Department> Departments { get; set; }
        public IEnumerable<Team> Teams { get; set; }
        public IEnumerable<Position> Positions { get; set; }

        [Display(ResourceType = typeof(Resources.Models.EmployeeInfo), Name = "Positions")]
        public IEnumerable<PostDetail> PostDetails { get; set; }

        public PostDetail PrimaryPosition
        {
            get
            {
                var result = new FB.Data.Models.PostDetail();
                if (null != PostDetails)
                {
                    var p = PostDetails.Where(w => w.IsPrimary).FirstOrDefault();
                    if (null != p)
                        result = p;
                    else
                        result = PostDetails.FirstOrDefault();
                }

                return result;
            }
                 
        }

        string GetAllItemName<T>(IEnumerable<T> Items) where T : CodeValuePair
        {
            string result = string.Empty;
            if (null != Items)
                result = string.Join(",", Items.Select(s => s.Name).Distinct().ToArray());

            return result;
        }
    }

    [Serializable]
    public class EmployeeInfo : EmployeeInfoBase
    {
        public List<EmployeeInfoLite> Subordinates { get; set; }
        public List<EmployeeInfoLite> Supervisors { get; set; }


        public bool IsSupervisingTo(string SubordinateCode)
        {
            return null != this.Subordinates && this.Subordinates.Exists(w => w.EMPCode == SubordinateCode);
        }

        public bool IsSubordinateTo(string SupervisorCode)
        {
            return null != this.Supervisors && this.Supervisors.Where(w => w.EMPCode == SupervisorCode).Count() > 0;
        }

    }
}
